package jmine.tec.web.servlet;

import java.io.IOException;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;

/**
 * Versoes de servlet-api anteriores a 3.0 nao expoem o status code da response http. Essa classe permite ter acesso a esse status.
 * 
 * @author Marcio Ribeiro (mmr)
 * @created Dec 9, 2011
 */
public class StatusExposingHttpServletResponse extends HttpServletResponseWrapper {

    private int status = SC_OK;

    /**
     * Constructor.
     * 
     * @param response http response.
     */
    public StatusExposingHttpServletResponse(HttpServletResponse response) {
        super(response);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void sendError(int sc) throws IOException {
        this.status = sc;
        super.sendError(sc);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void sendError(int sc, String msg) throws IOException {
        this.status = sc;
        super.sendError(sc, msg);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void setStatus(int sc) {
        this.status = sc;
        super.setStatus(sc);
    }

    /**
     * @return devolve o status guardado.
     */
    public int getStatus() {
        return this.status;
    }
}
